;(function() {
	
	Nokia.ResultsView = Nokia.View.extend({
		
		/*
		 * Initializes the ResultsView components
		 */
		init: function()
		{
			this.resultsList = this.getContainer().find('#results-list');
			
			var instance = this;
			
			this.weatherCarousel = new Nokia.Carousel({
				element: '#weather-carousel',
				visibleItems: 5,
				itemDimensions: {
					width: 48,
					height: 48
				},
				delay: 150,
				select: function() { instance.gotoWeatherView(this.selectedItem.getAttribute('dateIndex')); }
			});

		},
		
		/*
		 * Requests the WeatherView to be shown with the given date index
		 */
		gotoWeatherView: function(dateIndex)
		{
			this.getTemplate().getViewManager().show(Nokia.VIEW_WEATHER, dateIndex);
		},
		
		/*
		 * Called when the view needs to be rendered
		 */
		renderUI: function() {
			var instance = this;
			
			instance.resultsFilter = new Nokia.DropDown({
				element: '#results-filter',
				
				items: [
					{
						label: "All",
						value: 'all',
						select: function(item, event) {
							instance.filterResults('all');
						}
					},
					{
						label: "Twitter",
						value: 'twitter',
						select: function(item, event) {
							instance.filterResults('twitter');
						}
					},
					{
						label: "Picasa",
						value: 'picasa',
						select: function(item, event) {
							instance.filterResults('picasa');
						}
					},
					{
						label: "Google News",
						value: 'news',
						select: function(item, event) {
							instance.filterResults('news');
						}
					},
					{
						label: "Wikipedia",
						value: 'wikipedia',
						select: function(item, event) {
							instance.filterResults('wikipedia');
						}
					}
				]
			});
		},
		
		/*
		 * Filters the results depending on the selected type
		 */
		filterResults: function(filterType)
		{
			if(filterType == 'all')
			{
				$('.result-item').css({
					'display': ''
				});
			}
			else
			{
				$('.result-item').css({
					'display': 'none'
				});
				$('.result-item-' + filterType).css({
					'display': ''
				});
			}
		},
		
		/*
		 * Called when this view needs to be shown
		 */
		show: function()
		{
			// Replace the Exit button with a Back button
			
			var template = this.getTemplate();
			
			var topBar = template.getHeaderTopBar();
			var viewManager = template.getViewManager();
			
			var defaultTopItems = GBC.getDefaultTopItems();
			
			defaultTopItems[0] = {classname: 'nokia-template-header-icon-back', callback: function() { viewManager.back() }};
			
			topBar.setItems(defaultTopItems);
			
			this.getContainer().show();
		},
		
		/*
		 * Populates the weather Carousel component with the retrieved data
		 * The Carousel shows a single weather symbol for each day
		 * Clicking on a weather symbol opens up the Weather detail view
		 */
		showWeather: function(items)
		{
			var itemsHTML = '';
			
			var dates = new Array();
			var days = new Array();
			var dateIndex = 0;
			
			// Loop the weather items to create the HTML code for the Carousel items
			
			for(var i = 0; i < items.length; i++)
			{
				var item = items[i];
				
				var day = item.day;
				
				// Check if a symbol is defined and if this day has already been considered
				
				if(jQuery.inArray(day, days) == -1 && item.symbol_number)
				{
					itemsHTML += '<li dateIndex="' + dateIndex + '"><span class="weather-day">' + day + '</span>' + 
						'<img src="http://api.yr.no/weatherapi/weathericon/1.0/?symbol=' + item.symbol_number + ';content_type=image/png">' + 
						'</li>';
					
					days.push(day);
					dates.push(item.from);
					dateIndex++;
				}
			}

			this.weatherCarousel.element.html(itemsHTML);
			
			var instance = this;
			
			// Create the Carousel Guarana items
			
			var listItems = this.weatherCarousel.element.find(' > li');
			
			jQuery.each(listItems, function(i, item) {
				instance.weatherCarousel.addItem(jQuery(item));
			});
			
			// Select the first Carousel item
			
			instance.weatherCarousel.selectedItem = listItems[(0)];

			// Pass the weather data to the WeatherView instance
			
			var weatherView = this.getTemplate().getViewManager().getViews()[Nokia.VIEW_WEATHER];
			
			if(weatherView)
			{
				weatherView.setDates(dates);

				weatherView.setItems(items);
			}
		},
		
		/*
		 * Returns the result item of given index and type
		 */
		getResultItem: function(itemType, itemIndex)
		{
			return this.resultItems[itemType][itemIndex];
		},
		
		/*
		 * Shows the results retrieved from the Web services
		 */
		showResults: function(items)
		{
			this.resultItems = items;
			
			// Populate the Weather carousel, or hide it if no data is available
			
			if(items[Nokia.SERVICE_WEATHER])
			{
				this.showWeather(items[Nokia.SERVICE_WEATHER]);
				
				$('#weather').show();
			}
			else
			{
				$('#weather').hide();
			}
			
			// Since different Web services return different amount of results 
			// find the maximum number of returned results (to be used in the following loop)
			
			var maxIndex = 0;
			
			jQuery.each(items, function(i, results) {
				if(i != Nokia.SERVICE_WEATHER)
					maxIndex = Math.max(maxIndex, results.length)
			});
			
			// Create the HTML results list
			
			var itemsHTML = '';
			
			for(var i = 0; i < maxIndex; i++)
			{
				for(var j = 0; j < Nokia.SERVICE_LIST.length; j++)
				{
					if(j != Nokia.SERVICE_WEATHER)
					{
						var service = Nokia.SERVICE_LIST[j];
					
						var results = items[j];
						
						if(results && i < results.length)
						{						
							itemsHTML += Nokia.GBCServices.getServiceItemSnippet(results[i]);
						}
					}
				}
			}

			this.resultsList.html(itemsHTML);
			
			// Unbind any previous click event defined on the results list
			
			this.resultsList.unbind('click');
			
			// Bind the click event to the results list
			
			var instance = this;
			
			this.resultsList.click(function(event)
			{
				var target = jQuery(event.target);
				
				var resultItem = target.is('.result-item') ? target : target.parents('.result-item:first');
				
				var itemType = parseInt(resultItem.attr('itemtype'));
				
				var itemIndex = resultItem.attr('itemindex');
				
				var item = instance.getResultItem(itemType, itemIndex);
				
				// Depending on the clicked item, a different detail view has to be displayed
				
				var detailView = null;
				
				switch(itemType)
				{
					case Nokia.SERVICE_NEWS:
						detailView = Nokia.VIEW_NEWS;
						break;
					case Nokia.SERVICE_PICASA:
						detailView = Nokia.VIEW_PICASA;
						break;
					case Nokia.SERVICE_TWITTER:
						detailView = Nokia.VIEW_TWITTER;
						break;
					case Nokia.SERVICE_WIKIPEDIA:
						detailView = Nokia.VIEW_WIKIPEDIA;
						break;
					default:
						break;
				}

				if(detailView != null)
					instance.getTemplate().getViewManager().show(detailView, item);
			});
		}
	});
})();